package com.zb.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zb.dto.BrandDTO;
import com.zb.dto.CategoryDTO;
import com.zb.dto.SkuDTO;
import com.zb.dto.SpuDTO;
import com.zb.entity.TbBrandModel;
import com.zb.entity.TbCategoryModel;
import com.zb.entity.TbSkuModel;
import com.zb.entity.TbSpuModel;
import com.zb.service.TbBrandService;
import com.zb.service.TbCategoryService;
import com.zb.service.TbSkuService;
import com.zb.service.TbSpuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName TbSkuController
 * @Description 商品表控制器
 * @Author xm
 * @Date 2024/11/06 14:27
 **/
@RestController
@RequestMapping("/tb-sku-model")
public class TbSkuController {
    @Autowired
    public TbSkuService tbSkuModelService;

    @Autowired
    private TbSpuService tbSpuService;
    @Autowired
    private TbCategoryService tbCategoryService;
    @Autowired
    private TbBrandService tbBrandService;

    @Autowired
    private ConfigurableApplicationContext applicationContext;

    @GetMapping("/item")
    public Map<String,Object> item(@RequestParam("skuId") String skuId){
        Map<String, Object> map = new HashMap<>();
        TbSkuModel skuModel = tbSkuModelService.getById(skuId);
        TbSpuModel spuModel = tbSpuService.getById(skuModel.getSpuId());
        TbBrandModel brandModel = tbBrandService.getById(spuModel.getBrandId());
        TbCategoryModel category1Model = tbCategoryService.getById(spuModel.getCategory1Id());
        TbCategoryModel category2Model = tbCategoryService.getById(spuModel.getCategory2Id());
        TbCategoryModel category3Model = tbCategoryService.getById(spuModel.getCategory3Id());

        SkuDTO skuDTO = new SkuDTO();
        BeanUtils.copyProperties(skuModel, skuDTO);
        SpuDTO spuDTO = new SpuDTO();
        BeanUtils.copyProperties(spuModel, spuDTO);
        BrandDTO brandDTO = new BrandDTO();
        BeanUtils.copyProperties(brandModel, brandDTO);
        CategoryDTO c1 = new CategoryDTO();
        BeanUtils.copyProperties(category1Model, c1);
        CategoryDTO c2 = new CategoryDTO();
        BeanUtils.copyProperties(category2Model, c2);
        CategoryDTO c3 = new CategoryDTO();
        BeanUtils.copyProperties(category3Model, c3);
        map.put("skuDTO", skuDTO);
        map.put("spuDTO", spuDTO);
        map.put("brandDTO", brandDTO);
        map.put("c1", c1);
        map.put("c2", c2);
        map.put("c3", c3);
        return map;
    }


    @GetMapping("/page")
    public List<SkuDTO> page(@RequestParam("index") Integer index,
                             @RequestParam("size") Integer size) {
//        Page<TbSkuModel> page = new Page<>();
//        page.setCurrent(index.longValue());
//        page.setSize(size.longValue());
//        System.out.print("调用资源前");
//        tbSkuModelService.page(page);
//        System.out.print("已经查询到数据");
//        List<TbSkuModel> records = page.getRecords();
//        System.out.println("返回数据");
////        System.out.println(collect);
//        return records.stream().map(item -> {
//            SkuDTO skuDTO = new SkuDTO();
//            BeanUtils.copyProperties(item, skuDTO);
//            return skuDTO;
//        }).collect(Collectors.toList());
        return tbSkuModelService.getList(index,size);
    }


    @GetMapping("/path")
    public String getPath() {
        return applicationContext.getEnvironment().getProperty("upload.path");
    }

    @GetMapping("/info")
    public SkuDTO info(@RequestParam("id") String id) {
        TbSkuModel skuModel = tbSkuModelService.getById(id);
        SkuDTO skuDTO = new SkuDTO();
        BeanUtils.copyProperties(skuModel, skuDTO);
        return skuDTO;
    }

    @PostMapping("/update-num")
    public Boolean updateNum(@RequestParam("id") String id, @RequestParam("num") Integer num) {
        TbSkuModel skuModel = tbSkuModelService.getById(id);
        skuModel.setNum(skuModel.getNum() - num);
        return tbSkuModelService.updateById(skuModel);
    }


}
